#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sqlite3.h>
#include "door.h"
#include "doorinfo.h"
#include "portvictoria.h"

extern struct playerdata_t *player;
extern int port_count;
extern struct portdata_t **ports;

int display_current_location(void) {
	FILE *fptr;
	char c;
	int i;
	fptr = fopen("docks.ans", "r");
	
	if (!fptr) {
		// ....
	} else {
		c = fgetc(fptr);
		while (!feof(fptr)) {
			drprintf("%c", c);
			c = fgetc(fptr);
		}
		fclose(fptr);
	}
	
	
	drprintf("\e[1;33mYou're standing on the docks of %s.\r\n\r\n", player->current_port->name);
	for (i=0;i<strlen(player->current_port->description);i++) {
		if (player->current_port->description[i] == '\n') {
			drprintf("\r\n");
		} else {
			drprintf("%c", player->current_port->description[i]);
		}
	}
	drprintf("\r\n\r\n");
	drprintf("Your %s has %d of %d cargo spaces free. You have %d gold coins.\r\n\r\n", player->ship->name, player->ship->cargo_units - total_cargo_spaces_used(), player->ship->cargo_units, player->dollars);
	drprintf("        \e[1;37m(\e[1;33mM\e[1;37m) Enter the Marketplace\r\n");
	drprintf("        \e[1;37m(\e[1;33mT\e[1;37m) Visit the Tavern\r\n");
	drprintf("        \e[1;37m(\e[1;33mS\e[1;37m) Visit the Shipyard\r\n");
	drprintf("        \e[1;37m(\e[1;33mC\e[1;37m) Continue voyage\r\n\r\n");
	drprintf("        \e[1;37m(\e[1;31mQ\e[1;37m) Quit to BBS\r\n");
	     
	while (1) {
		switch(tolower(drgetc())) {
			case 'm':
				return 1;
			case 't':
				return 4;
			case 'c':
				return 2;
			case 'q':
				return 3;
			case 's':
				return 5;
		}
	}
}

int select_destination(void) {
	struct portdata_t *dest1 = NULL;
	struct portdata_t *dest2 = NULL;
	struct portdata_t *dest3 = NULL;
	struct portdata_t *dest4 = NULL;
	
	int i;
	char c;
	
	for (i=0;i<port_count;i++) {
		if (ports[i]->id == player->current_port->location1) {
			dest1 = ports[i];
		}
		if (ports[i]->id == player->current_port->location2) {
			dest2 = ports[i];
		}
		if (ports[i]->id == player->current_port->location3) {
			dest3 = ports[i];
		}
		if (ports[i]->id == player->current_port->location4) {
			dest4 = ports[i];
		}
	}
	
	while (1) {
	
		drprintf("\e[2J\e[1;33mStudying your map, you see four nearby destinations.\r\nYou have %d months of travel left this turn.\r\n\r\n", MONTHS_PER_DAY - player->months_used);
	
		if (dest1 != NULL) {
			drprintf("        \e[1;37m(\e[1;33m1\e[1;37m) %s (%d months travel time)\r\n", dest1->name, player->current_port->location1cost);
		}
		if (dest2 != NULL) {
			drprintf("        \e[1;37m(\e[1;33m2\e[1;37m) %s (%d months travel time)\r\n", dest2->name, player->current_port->location2cost);
		}
		if (dest3 != NULL) {
			drprintf("        \e[1;37m(\e[1;33m3\e[1;37m) %s (%d months travel time)\r\n", dest3->name, player->current_port->location3cost);
		}
		if (dest4 != NULL) {
			drprintf("        \e[1;37m(\e[1;33m4\e[1;37m) %s (%d months travel time)\r\n", dest4->name, player->current_port->location4cost);
		}
	
		drprintf("\r\n        \e[1;37m(\e[1;33mR\e[1;37m) Return to the docks\r\n");
		drprintf("        \e[1;37m(\e[1;31mQ\e[1;37m) Quit to BBS\r\n");
	
		c = drgetc();
		while (c == -1) {
			usleep(100);
			c = drgetc();
		}
	
		switch (c) {
			case '1':
				if (dest1 != NULL) {
					if (MONTHS_PER_DAY - player->months_used >= player->current_port->location1cost) {
						player->months_used += player->current_port->location1cost;
						player->current_port = dest1;
						return 1;
					} else {
						drprintf("Not enough months left.\r\n");
					}
				}
				break;
			case '2':
				if (dest2 != NULL) {
					if (MONTHS_PER_DAY - player->months_used >= player->current_port->location2cost) {
						player->months_used += player->current_port->location2cost;
						player->current_port = dest2;
						return 1;
					} else {
						drprintf("Not enough months left.\r\n");
					}
				}
				break;
			case '3':
				if (dest3 != NULL) {
					if (MONTHS_PER_DAY - player->months_used >= player->current_port->location3cost) {
						player->months_used += player->current_port->location3cost;
						player->current_port = dest3;
						return 1;
					} else {
						drprintf("Not enough months left.\r\n");
					}
				}
				break;
			case '4':
				if (dest4 != NULL) {
					if (MONTHS_PER_DAY - player->months_used >= player->current_port->location4cost) {
						player->months_used += player->current_port->location4cost;
						player->current_port = dest4;
						return 1;
					} else {
						drprintf("Not enough months left.\r\n");
					}
				}
				break;
			case 'q':
			case 'Q':
				return 0;
			case 'r':
			case 'R':
				return 1;
		}
	}
}
